package com.wiz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.wiz.common.Constant;
import com.wiz.mapper.StationCodeMapper;
import com.wiz.mapper.StationMapper;
import com.wiz.service.StationService;
import com.wiz.to.StationCodeTO;
import com.wiz.to.StationPhotoTO;
import com.wiz.to.StationTO;
import com.wiz.util.FileUtil;
import com.wiz.util.HttpUtil;
import com.wiz.vo.criteria.StationSearchCriteriaTO;
//@Transactional
@Service("stationService")
public class StationServiceImpl implements StationService {
	private transient final Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	private StationMapper stationMapper;
	@Resource
	private StationCodeMapper stationCodeMapper;

	@Override
	@Order(value=2)
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public void addStationAddImg(StationTO stationTO, MultipartFile saImg,
			MultipartFile cdImg, MultipartFile tankerImg, MultipartFile hnImg,
			MultipartFile CVSImg,MultipartFile WCImg,
			MultipartFile CWImg,MultipartFile repairImg)
			throws Exception {
		int random = Constant.STATION_RANDOM;
		String hqId = stationTO.getHqId();
		String stationId = stationMapper.getMaxHqStationId(hqId);
		if(stationId != null && !stationId.equals("")){
			String oldRandom = stationId.substring(stationId.length()-String.valueOf(random).length());	
			random = Integer.valueOf(oldRandom)+1;
		}
		stationId = hqId+random;
		stationTO.setStationId(stationId);
			List <StationPhotoTO> photos = saveImg(stationTO, saImg, cdImg, tankerImg, hnImg,
					CVSImg,WCImg,CWImg,repairImg);
			stationMapper.addStation(stationTO);
			stationMapper.addBaseInfo(stationTO);
			if(photos.size()>0){
				stationMapper.addPhotos(photos);
			}
		
	}

	@Override
	public List<StationTO> getListHq() {
		return stationMapper.getListHq();
	}

	@Override
	public List<StationTO> getStationsByCriteriaTO(StationSearchCriteriaTO stationSearchCriteriaTO) {
		// TODO Auto-generated method stub
		return stationMapper.getStationsByCriteriaTO(stationSearchCriteriaTO);
	}

	@Override
	public int getStationsCntByCriteriaTO(StationSearchCriteriaTO stationSearchCriteriaTO) {
		// TODO Auto-generated method stub
		return stationMapper.getStationsCntByCriteriaTO(stationSearchCriteriaTO);
	}
	@Override
	public StationTO getStation(String stationId) {
		List<StationTO>list = stationMapper.getStation(stationId);
		if(list != null && list.size()>0){
			return list.get(0);
		}
		return null;
	}
	@Override
	public Map<Integer,String> getStationPhoto(String stationId) {
		Map<Integer,String>map = new HashMap<Integer, String>();
		List<StationPhotoTO> list = stationMapper.listStationPhoto(stationId);
		for(int i = 0; i<list.size();i++){
			map.put(list.get(i).getType(), list.get(i).getUrl());
		}
		return map;
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public StationTO updateStationAddImg(StationTO stationTO, MultipartFile saImg,
			MultipartFile cdImg, MultipartFile tankerImg, MultipartFile hnImg,
			MultipartFile CVSImg,MultipartFile WCImg,
			MultipartFile CWImg,MultipartFile repairImg)throws Exception {
		StationPhotoTO photo = null;
		List<StationPhotoTO> sphotos = null;
		List<StationPhotoTO> list = new ArrayList<StationPhotoTO>();
		//保存图片
		List <StationPhotoTO> photos = saveImg(stationTO, saImg, cdImg, tankerImg, hnImg,
				CVSImg,WCImg,CWImg,repairImg);
		stationMapper.updateStation(stationTO);
		stationMapper.updateBaseInfo(stationTO);

		for(int i=0;i<photos.size();i++){
			photo = photos.get(i);
			sphotos = stationMapper.getPhoto(photo.getStationId(), photo.getType());
			if(sphotos != null && sphotos.size()>0){
				stationMapper.updatePhotos(photo);
			}else{
				photo.setCreatedTime(new Date());
				list.add(photo);
			}
		}
		if(list.size()>0){
			stationMapper.addPhotos(list);
		}
		return stationTO;
	}
	
	@Override
	public List<StationPhotoTO> saveImg(StationTO stationTO,MultipartFile saImg,
			MultipartFile cdImg, MultipartFile tankerImg, MultipartFile hnImg,
			MultipartFile CVSImg,MultipartFile WCImg,
			MultipartFile CWImg,MultipartFile repairImg)throws Exception {
		//保存图片
		List<StationPhotoTO>photos = new ArrayList<StationPhotoTO>();
		String url = null;
		
		if(saImg != null &&saImg.getSize()> 0){
			url = FileUtil.uploadFile(saImg,Constant.STATION_DIRECTORY,stationTO.getStationId(),1);
			photos.add(new StationPhotoTO(stationTO, 1, url));
		}
		if(cdImg != null &&cdImg.getSize()> 0){
			url = FileUtil.uploadFile(cdImg,Constant.STATION_DIRECTORY,stationTO.getStationId(),2);
			photos.add(new StationPhotoTO(stationTO, 2, url));
		}
		if(tankerImg != null &&tankerImg.getSize()> 0){
			url = FileUtil.uploadFile(tankerImg,Constant.STATION_DIRECTORY,stationTO.getStationId(),3);
			photos.add(new StationPhotoTO(stationTO, 3, url));
		}
		if(hnImg != null &&hnImg.getSize()> 0){
			url = FileUtil.uploadFile(hnImg,Constant.STATION_DIRECTORY,stationTO.getStationId(),4);
			photos.add(new StationPhotoTO(stationTO, 4, url));
		}
		if(CVSImg != null &&CVSImg.getSize()> 0){
			url = FileUtil.uploadFile(CVSImg,Constant.STATION_DIRECTORY,stationTO.getStationId(),5);
			photos.add(new StationPhotoTO(stationTO, 5, url));
		}
		if(WCImg != null &&WCImg.getSize()> 0){
			url = FileUtil.uploadFile(WCImg,Constant.STATION_DIRECTORY,stationTO.getStationId(),6);
			photos.add(new StationPhotoTO(stationTO, 6, url));
		}
		if(CWImg != null &&CWImg.getSize()> 0){
			url = FileUtil.uploadFile(CWImg,Constant.STATION_DIRECTORY,stationTO.getStationId(),7);
			photos.add(new StationPhotoTO(stationTO, 7, url));
		}
		if(repairImg != null &&repairImg.getSize()> 0){
			url = FileUtil.uploadFile(repairImg,Constant.STATION_DIRECTORY,stationTO.getStationId(),8);
			photos.add(new StationPhotoTO(stationTO, 8, url));
		}
				
		return photos;
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public void remove(String stationId)throws Exception {
		stationMapper.removePhoto(stationId);
		stationMapper.removeBaseInfo(Integer.valueOf(stationId));
		stationMapper.removeStation(Integer.valueOf(stationId));
	}

	@Override
	public String createCode(String stationId)throws Exception {
		String url = Constant.CREATE_CODE_URL;
		//测试，生成的是一个临时二维码
		url = url.replace("ACTIONNAME", "QR_SCENE");
		url = url.replace("SCENEID", stationId);
		url = url.replace("SCENESTR", "");
		HttpUtil util = new HttpUtil();
		String response = util.getHttpsResponseByGet(url);
		net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(response);
		if(jsonObject.getInt("code")==200){
			url = jsonObject.getString("msg");
			log.info("code_url===============>"+response);
			StationCodeTO stationCode = new StationCodeTO(stationId, url);
			stationCodeMapper.addStationCode(stationCode);
		}else{
			throw new Exception("获取二维码失败！");
		}
		return url;
	}
	@Override
	public StationCodeTO getCode(String stationId)throws Exception {
		
		List<StationCodeTO>list = stationCodeMapper.queryByStationId(stationId);
		if(list != null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<StationCodeTO> ListCode(List<String> list) throws Exception {
		
		return stationCodeMapper.ListCode(list);
	}
	
	
}